package cn.iocoder.yudao.module.crossborder.finance.controller.admin;

import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.module.crossborder.finance.api.dto.ExchangeRateDTO;
import cn.iocoder.yudao.module.crossborder.finance.api.dto.SettlementDTO;
import cn.iocoder.yudao.module.crossborder.finance.service.FinanceService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.math.BigDecimal;
import java.util.List;

import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

/**
 * 管理后台 - 财务结算管理
 *
 * @author LHL
 */
@Tag(name = "管理后台 - 财务结算管理")
@RestController
@RequestMapping("/crossborder/finance")
@Validated
public class FinanceController {

    @Resource
    private FinanceService financeService;

    @GetMapping("/settlement/get")
    @Operation(summary = "获得财务结算记录")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('crossborder:finance:query')")
    public CommonResult<SettlementDTO> getSettlement(@RequestParam("id") Long id) {
        SettlementDTO settlement = financeService.getSettlement(id);
        return success(settlement);
    }

    @GetMapping("/settlement/list")
    @Operation(summary = "获得财务结算记录列表")
    @Parameter(name = "ids", description = "编号列表", required = true, example = "1024,2048")
    @PreAuthorize("@ss.hasPermission('crossborder:finance:query')")
    public CommonResult<List<SettlementDTO>> getSettlementList(@RequestParam("ids") List<Long> ids) {
        List<SettlementDTO> settlements = financeService.getSettlementList(ids);
        return success(settlements);
    }

    @PostMapping("/settlement/create")
    @Operation(summary = "创建财务结算记录")
    @PreAuthorize("@ss.hasPermission('crossborder:finance:create')")
    public CommonResult<Long> createSettlement(@RequestBody @Valid SettlementDTO settlement) {
        Long id = financeService.createSettlement(settlement);
        return success(id);
    }

    @GetMapping("/exchange-rate/get")
    @Operation(summary = "获取汇率")
    @Parameter(name = "fromCurrency", description = "源货币", required = true, example = "USD")
    @Parameter(name = "toCurrency", description = "目标货币", required = true, example = "CNY")
    @PreAuthorize("@ss.hasPermission('crossborder:finance:query')")
    public CommonResult<ExchangeRateDTO> getExchangeRate(@RequestParam("fromCurrency") String fromCurrency,
                                                         @RequestParam("toCurrency") String toCurrency) {
        ExchangeRateDTO exchangeRate = financeService.getExchangeRate(fromCurrency, toCurrency);
        return success(exchangeRate);
    }

    @PostMapping("/fee/calculate")
    @Operation(summary = "计算费用")
    @Parameter(name = "orderId", description = "订单编号", required = true, example = "1024")
    @Parameter(name = "feeType", description = "费用类型", required = true, example = "1")
    @PreAuthorize("@ss.hasPermission('crossborder:finance:calculate')")
    public CommonResult<BigDecimal> calculateFee(@RequestParam("orderId") Long orderId,
                                                @RequestParam("feeType") Integer feeType) {
        BigDecimal fee = financeService.calculateFee(orderId, feeType);
        return success(fee);
    }

    @PostMapping("/settlement/process")
    @Operation(summary = "处理结算")
    @Parameter(name = "orderId", description = "订单编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('crossborder:finance:process')")
    public CommonResult<Boolean> processSettlement(@RequestParam("orderId") Long orderId) {
        Boolean result = financeService.processSettlement(orderId);
        return success(result);
    }

}
